﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace GoogleSync
{
    public enum Fields
    {
        TITLE,
        FIRST,
        MIDDLE,
        LAST,
        NICKNAME,
        BUSINESS_PHONE,
        BUSINESS_PHONE2,
        MOBILE_PHONE,
        MOBILE_PHONE2,
        HOME_PHONE,
        HOME_PHONE2,
        OTHER_PHONE,
        EMAIL1,
        EMAIL2,
        EMAIL3,
        COMPANY,
        BUSINESS_ADDR,
        HOME_ADDR,
        OUTID
    }

    [Serializable]
    abstract class AContact
    {

        public delegate void SetFunction(String value);
        public delegate String GetFunction();

        protected Dictionary<Fields, SetFunction> setFields = new Dictionary<Fields, SetFunction>();
        protected Dictionary<Fields, GetFunction> getFields = new Dictionary<Fields, GetFunction>();

        protected bool pictureChanged = false;

        public bool changed = false;

        public virtual bool compareTo(AContact contact)
        {
            foreach (Fields field in Enum.GetValues(typeof(Fields)))
            {
                if (!this.getField(field).Equals(contact.getField(field)))
                    return false;
            }
            return true;
        }

        public virtual String getField(Fields field)
        {
            String temp = getFields[field]();
            return temp == null ? "" : temp;
        }

        public virtual void setField(Fields field, String value)
        {
            changed = true;
            setFields[field](value);
        }

        public virtual void updateWith(AContact contact, ReferenceContact refContact)
        {
            foreach (Fields field in Enum.GetValues(typeof(Fields)))
            {
                this.setField(field, contact.getField(field));
            }
            this.setPictureBytes(contact.getPictureBytes(refContact.getGooglePictureHash()));
        }

        public abstract void save();

        public abstract void delete();

        public abstract String getPictureHash();

        public abstract byte[] getPictureBytes(String lastEtag);

        public abstract void setPictureBytes(byte[] pictureBytes);

        public String getHashFromBytes(byte[] pictureBytes)
        {
            if (pictureBytes == null) return "";
            int index;
            int fileLength = pictureBytes.GetLength(0);
            int[] hash = new int[10];
            for (index = 0; index < fileLength; index++)
            {
                hash[index % 10] += pictureBytes[index];
            }
            char[] hashChars = new char[10];
            for (index = 0; index < 10; index++)
            {
                hashChars[index] = (char)('a' + (Math.Abs(hash[index]) % 25));
            }
            return new String(hashChars);

        }

        public virtual bool getDeleted()
        {
            return false;
        }


    }
}
